﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Runtime.Serialization;

namespace Utility
{
    public static class Serializer
    {
        public static void Serialize<T>(T obj, string path)
        {
            DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
            FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write);
            serializer.WriteObject(stream, obj);
            stream.Close();
            /*
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Write);
            serializer.Serialize(stream, obj);
            stream.Close();*/
        }

        public static void Deserialize<T>(ref T obj, string path)
        {
            DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
            FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);

            obj = (T)serializer.ReadObject(stream);
            stream.Close();

            /*
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
            obj = (T)serializer.Deserialize(stream);
            stream.Close();*/
        }
    }
}
